package com.briskemen.client;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.briskemen.service.AddNumber;
import com.briskemen.service.Book;
import com.briskemen.service.BookController;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private BookController bookController;
    private boolean        connected;
    private List<Book>     bookList;

    private AddNumber      mAddNumber;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            bookController = BookController.Stub.asInterface(iBinder);
            //这里会冲突，不解
           // mAddNumber = AddNumber.Stub.asInterface(iBinder);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            connected = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //bindAddService();
       bindService();
    }

    /**
     * 会冲突，不太理解
     */
    private void bindAddService() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.briskemen.service",
                "com.briskemen.service.AddNumberService"));
        bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
    }

    /**
     * 绑定服务
     */
    private void bindService() {
        Intent intent = new Intent();
        intent.setPackage("com.briskemen.service");
        intent.setAction("com.briskemen.service.action");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }


    public void aidlTest(View view){
        if (connected) {
            try {
                bookList = bookController.getBookList();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            log();
        }
    }

    public void aidlTest2(View view){
        if (connected) {
            Book book = new Book("这是一本新书 InOut");
            try {
                bookController.addBookInOut(book);
                Log.e(TAG, "向服务器以InOut方式添加了一本新书");
                Log.e(TAG, "新书名：" + book.getName());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            log();
        }
    }

    /**
     * 通过aidl进行加法运算
     * @param view
     */
    public void addNumber(View view){
       startActivity(new Intent(this,Main2Activity.class));

        //这里与上面connect绑定服务相关，但上面似乎不能同时绑定两个不同的service，aidl文件
        /*try {
            int total = mAddNumber.add(3, 4);
            Log.e(TAG,"total-------->"+total);
        } catch (RemoteException e) {
            e.printStackTrace();
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connected) {
            unbindService(mConnection);
        }
    }

    private void log() {
        for (Book book : bookList) {
            Log.e(TAG, book.toString());
        }
    }

}
